﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows;

namespace Dotway.WPF.Controls.Utilities
{
    internal class ColorBarData
    {
        private Color[] colors = new Color[7];

        public ColorBarData()
        {
            colors[0] = Color.FromRgb(255, 0, 0);
            colors[1] = Color.FromRgb(255, 255, 0);
            colors[2] = Color.FromRgb(0, 255, 0);
            colors[3] = Color.FromRgb(0, 255, 255);
            colors[4] = Color.FromRgb(0, 0, 255);
            colors[5] = Color.FromRgb(255, 0, 255);
            colors[6] = Color.FromRgb(255, 0, 1);
        }

        public Brush GetHorizontalBackground()
        {
            LinearGradientBrush brush = new LinearGradientBrush();
            brush.StartPoint = new Point(0, 0.5);
            brush.EndPoint = new Point(1, 0.5);

            for (int i = 0; i < colors.Length; i++)
            {
                GradientStop gradientStop = new GradientStop(colors[i], (double)i / (colors.Length - 1));
                brush.GradientStops.Add(gradientStop);
            }

            return brush;
        }

        public Brush GetVerticalBackground()
        {
            LinearGradientBrush brush = new LinearGradientBrush();
            brush.StartPoint = new Point(0.5, 0.0);
            brush.EndPoint = new Point(0.5, 1.0);

            for (int i = 0; i < colors.Length; i++)
            {
                GradientStop gradientStop = new GradientStop(colors[i], (double)i / (colors.Length - 1));
                brush.GradientStops.Add(gradientStop);
            }

            return brush;
        }

        /// <summary>
        /// Degree is a value in [0 - 360].
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        public Color GetColor(double degree)
        {
            if (degree > 360 || degree < 0.0)
            {
                throw new ArgumentOutOfRangeException("Degree is out of range. Must be [0 - 360].");
            }

            RGB rgb = Rgb2HsvConverter.Hsv2Rgb(new HSV(degree, 1.0, 1.0));
            Color color = Color.FromRgb(0,0,0);
            color.R = (byte) (rgb.R * 255);
            color.G = (byte) (rgb.G * 255);
            color.B = (byte) (rgb.B * 255);
            return color;
        }

        public double GetDegree(Color color)
        {
            return Rgb2HsvConverter.Color2H(color);
        }        
    }
}
